package com.xiaoxin.experience.service.http.storage;

import com.xiaoxin.experience.common.BaseSystemMessage;
import com.xiaoxin.experience.common.Property;
import com.xiaoxin.experience.util.CollectionUtil;
import com.xiaoxin.experience.util.FileUtil;

import java.io.File;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author xiaoxin
 */
public class EnvironmentFileHandler
{
    private static final String ENVIRONMENT_FILE_PATH = BaseSystemMessage.baseCachePath() + "environment.d";

    private static final Map<String, Map<String,String>> ENVIRONMENT_MAP = new ConcurrentHashMap<>();

    private static final String ENVIRONMENT_PREFIX = "---";

    static
    {
        init();
    }

    private static void init()
    {
        if (fileExists())
        {
            loadEnvironment();
            return;
        }
        initData();
    }

    private static void initData()
    {
        Map<String,String> map = new HashMap<>();
        map.put("prefix","http://172.16.20.71:9952");
        ENVIRONMENT_MAP.put("default",map);
        saveEnvironment();
    }

    private static void loadEnvironment()
    {
        List<String> strings = FileUtil.readAsList(ENVIRONMENT_FILE_PATH);
        Map<String,String> tempMap = null;
        String environmentName = null;
        for (String string : strings)
        {
            if (string.startsWith(ENVIRONMENT_PREFIX))
            {
                if (Objects.nonNull(environmentName))
                {
                    ENVIRONMENT_MAP.put(environmentName,tempMap);
                    environmentName = string.substring(ENVIRONMENT_PREFIX.length());
                }
                else
                {
                    environmentName = string.substring(ENVIRONMENT_PREFIX.length());
                }
                tempMap = new HashMap<>();
            }
            else
            {
                String[] split = string.split("=");
                if (Objects.isNull(tempMap) || split.length != 2)
                {
                    continue;
                }
                tempMap.put(split[0],split[1]);
            }
        }
        ENVIRONMENT_MAP.put(environmentName,tempMap);
    }

    public static void addProperty(String environmentName,String key,String value)
    {
        Map<String, String> map = ENVIRONMENT_MAP.get(environmentName);
        if (Objects.isNull(map))
        {
            return;
        }
        map.put(key,value);
        saveEnvironment();
    }

    public static void delProperty(String environmentName,String key)
    {
        Map<String, String> map = ENVIRONMENT_MAP.get(environmentName);
        if (CollectionUtil.isEmpty(map))
        {
            return;
        }
        map.remove(key);
        saveEnvironment();
    }

    public static void createNewEnvironment(String environmentName, List<Property> properties)
    {
        Map<String, String> map = ENVIRONMENT_MAP.get(environmentName);
        if (CollectionUtil.isEmpty(map))
        {
            map = new HashMap<>();
        }
        for (Property property : properties)
        {
            map.put(property.getKey(),property.getValue());
        }
        ENVIRONMENT_MAP.put(environmentName,map);
        saveEnvironment();
    }

    public static void deleteEnvironment(String environmentName)
    {
        Map<String, String> map = ENVIRONMENT_MAP.get(environmentName);
        if (CollectionUtil.isEmpty(map))
        {
            return;
        }
        ENVIRONMENT_MAP.remove(environmentName);
        saveEnvironment();
    }

    public static List<String> getEnvironmentNames()
    {
        return new ArrayList<>(ENVIRONMENT_MAP.keySet());
    }

    public static Map<String,String> getEnvironmentMessage(String environmentName)
    {
        return ENVIRONMENT_MAP.get(environmentName);
    }

    private static void saveEnvironment()
    {
        List<String> properties = new ArrayList<>();
        Set<String> environmentNames = ENVIRONMENT_MAP.keySet();
        for (String environmentName : environmentNames)
        {
            properties.add(ENVIRONMENT_PREFIX + environmentName);
            Map<String, String> map = ENVIRONMENT_MAP.get(environmentName);
            map.forEach((k,v)->properties.add(k+"="+v));
        }
        FileUtil.writeToFile(properties,ENVIRONMENT_FILE_PATH,false);
    }

    private static boolean fileExists()
    {
        File environmentFile = new File(ENVIRONMENT_FILE_PATH);
        return environmentFile.exists();
    }
}
